<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const reduce = (array, fn, init) => {
            let accumulate = init === undefined ? array[0] : init

            for (let index = 0; index < array.length; index++) {
                const element = array[index];
                accumulate = fn(accumulate, element)
            }
            return accumulate
        }
        const arr = [1, 2, 3]
        console.log(reduce(arr, (acc, cur) => acc + cur, 0));

    </script>
</body>

</html>